package frame.base;

import tk.mybatis.mapper.common.Mapper;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 基于tk.mapper实现的单表Service
 * <br>
 * @param <I>实体主键类型
 * @param <T>实体类型
 * @param <Q>查询对象类型
 */
public abstract class BaseService<I,T extends BaseTo,Q extends BaseQo> {
    /**
     * 获取模块的主Mapper
     * @return
     */
    abstract protected Mapper<T> mainMapper();

    protected void preAdd(T record) {
        record.setCreateTime(new Date());
        record.setUpdateTime(new Date());
    }
    protected void preUpdate(T record) {
        record.setUpdateTime(new Date());
    }

    protected abstract T newInstanceT(I id);

    public T add(T record) {
        if (record == null) throw new IllegalArgumentException("要创建的数据为NULL");
        this.preAdd(record);
        mainMapper().insert(record);
        return record;
    }
    public T addSelective(T record) {
        if (record == null) throw new IllegalArgumentException("要创建的数据为NULL");
        this.preAdd(record);
        mainMapper().insertSelective(record);
        return record;
    }

    /**
     * 物理删除，禁止物理删除overwrite此方法为空方法
     * @param id
     * @return
     */
    public int delete(I id) {
        if (id == null) throw new IllegalArgumentException("要删除数据的ID为NULL");
        return mainMapper().deleteByPrimaryKey(id);
    }
    public int delete(List<I> idList) {
        if (idList == null || idList.size() == 0) throw new IllegalArgumentException("要删除数据的IDList为空");
        int re = 0;
        for (I id : idList) re += this.delete(id);
        return re;
    }
    public int delete(T record) {
        if (record == null) throw new IllegalArgumentException("要删除的数据为NULL");
        return mainMapper().delete(record);
    }

    public int update(T record) {
        if (record == null) throw new IllegalArgumentException("要修改的数据为NULL");
        if (record.getId() == null) throw new IllegalArgumentException("要修改的数据的ID为NULL");
        this.preUpdate(record);
        return mainMapper().updateByPrimaryKey(record);
    }
    public int updateSelective(T record) {
        if (record == null) throw new IllegalArgumentException("要修改的数据为NULL");
        if (record.getId() == null) throw new IllegalArgumentException("要修改的数据的ID为NULL");
        this.preUpdate(record);
        return mainMapper().updateByPrimaryKeySelective(record);
    }

    /**
     * 逻辑删除 valid=0
     * @param id
     * @return
     */
    public int remove(I id) {
        if (id == null) throw new IllegalArgumentException("要移除数据的ID为NULL");
        T record = newInstanceT(id);
        record.setValid(BaseConstant.TO_VALID_NOT);
        return mainMapper().updateByPrimaryKeySelective(record);
    }
    public int remove(List<I> idList) {
        if (idList == null || idList.size() == 0) throw new IllegalArgumentException("要移除数据的IDList为空");
        int re = 0;
        for (I id : idList) re += this.remove(id);
        return re;
    }
    public T selectByPrimaryKey(I id) {
        if (id == null) throw new IllegalArgumentException("要查询数据的ID为NULL");
        return mainMapper().selectByPrimaryKey(id);
    }

    public abstract BootstrapTable<T> pageList(Q qo);

    public String uuid32() {
        return uuid36().replaceAll("-","");
    }

    public String uuid36() {
        return UUID.randomUUID().toString();
    }
}
